package io.renren.modules.vastFinance.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.vastFinance.dao.CouponDao;
import io.renren.modules.vastFinance.dao.InfoDao;
import io.renren.modules.vastFinance.entity.CouponEntity;
import io.renren.modules.vastFinance.entity.InfoEntity;
import io.renren.modules.vastFinance.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("infoService")
public class InfoServiceImpl extends ServiceImpl<InfoDao, InfoEntity> implements InfoService {

    @Autowired
    InfoDao infoDao;
    @Autowired
    CouponDao couponDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<InfoEntity> page = this.page(
                new Query<InfoEntity>().getPage(params),
                new QueryWrapper<InfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void sendInfo(Integer userId, String text) {
        // 增加一条消息
        InfoEntity infoEntity = new InfoEntity();
        infoEntity.setInfoName("您中奖了！！！");
        infoEntity.setNews("恭喜您获得【" + text + "】。");
        infoEntity.setUserId(userId);
        infoDao.insert(infoEntity);
        // 增加优惠券
        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponName(text);
        if (text.length() == 5) {
            couponEntity.setCouponPrice(Integer.parseInt(text.substring(2,4)));
        }else{
            couponEntity.setCouponPrice(Integer.parseInt(text.substring(2,3)));
        }
        couponEntity.setUserId(userId);
        couponDao.insert(couponEntity);
    }

    public static void main(String[] args) {
        String str = "免单5元";
        String substring = str.substring(2, 3);
        System.out.println(substring);
    }
}